
package com.loong.loong.hik.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.loong.common.core.util.R;
import com.loong.loong.hik.api.dto.HikFaceDto;
import com.loong.loong.hik.api.dto.HikFaceInfoDto;
import com.loong.loong.hik.api.dto.HikFacePicDto;
import com.loong.loong.hik.entity.Face;
import com.loong.loong.hik.entity.HikIndex;
import com.loong.loong.hik.mapper.FaceMapper;
import com.loong.loong.hik.property.request.face.FaceBatchDelRequestDto;
import com.loong.loong.hik.property.result.face.FaceGroupResultVo;
import com.loong.loong.hik.property.result.face.FaceResultVo;
import com.loong.loong.hik.property.urls.face.HikFaceUrl;
import com.loong.loong.hik.service.FaceService;
import com.loong.loong.hik.service.HikFaceVService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 人脸管理
 *
 * @author jinyiang
 * @date 2023-03-28 17:17:48
 */
@Service

public class FaceServiceImpl extends ServiceImpl<FaceMapper ,Face> implements FaceService{

	@Resource
	private HikFaceVService hikFaceVService;

	@Override
	public R add(Face face) {
		//调接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			HikFaceDto hikFaceDto = new HikFaceDto();
			hikFaceDto.setFaceGroupIndexCode(face.getFaceGroupIndexCode());

			HikFaceInfoDto hikFaceInfoDto = new HikFaceInfoDto();
			hikFaceInfoDto.setName(face.getName());
			hikFaceInfoDto.setSex(face.getSex());
			hikFaceInfoDto.setCertificateType(face.getCertificateType());
			hikFaceInfoDto.setCertificateNum(face.getCertificateNum());
			hikFaceDto.setFaceInfo(hikFaceInfoDto);
			HikFacePicDto hikFacePicDto = new HikFacePicDto();
			hikFacePicDto.setFaceUrl(face.getFaceUrl());
			hikFaceDto.setFacePic(hikFacePicDto);
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				FaceResultVo faceResultVo = hikFaceVService.add(hikFaceDto);
				if (faceResultVo==null){
					return R.failed("接口返回值为空！");
				}
				if ("0".equals(faceResultVo.getCode())){

					HikFaceDto data = faceResultVo.getData();
					if (data!=null){
						face.setIndexCode(data.getIndexCode());
					}
				}else{
					return R.failed("接口调用失败，原因："+faceResultVo.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		Face face1 = new Face();

		face1.setIndexCode(face.getIndexCode());

		long count = count(Wrappers.query(face1));

		if (count>0){
			return R.failed("已存在相同的编号,编号："+face.getIndexCode());
		}else{
			return R.ok(save(face));
		}
	}



/*	 * 批量删除人脸
	 * @param id
	 * @return
	 **/

	@Override
	public R remove(Integer id) {

		Face face = getById(id);
		if (face==null){
			R.failed("删除失败，根据id查询为空");
		}
		if (StringUtils.isEmpty(face.getIndexCode())){
			R.failed("删除失败，indexCode为空");
		}
		if (HikFaceUrl.SWITCH.equals("Y")){
			FaceBatchDelRequestDto faceBatchDelRequestDto = new FaceBatchDelRequestDto();
			List<String> strings = new ArrayList<>();
			strings.add(face.getIndexCode());
			faceBatchDelRequestDto.setIndexCodes(strings);
			try {
				FaceGroupResultVo faceGroupResultVo = hikFaceVService.batchDel(faceBatchDelRequestDto);
				if (faceGroupResultVo==null){
					return R.failed("接口返回值为空！");
				}
				if (!"0".equals(faceGroupResultVo.getCode())){
					return R.failed("接口调用失败，原因："+faceGroupResultVo.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok(removeById(id));
	}


	/**
	 * 修改人脸
	 * @param
	 * @return
	 */
	@Override
	public R update(Face face) {

		if (HikFaceUrl.SWITCH.equals("Y")){
			HikFaceDto hikFaceDto = new HikFaceDto();
			hikFaceDto.setIndexCode(face.getIndexCode());

			HikFaceInfoDto hikFaceInfoDto = new HikFaceInfoDto();
			hikFaceInfoDto.setName(face.getName());
			hikFaceInfoDto.setSex(face.getSex());
			hikFaceInfoDto.setCertificateType(face.getCertificateType());
			hikFaceInfoDto.setCertificateNum(face.getCertificateNum());
			hikFaceDto.setFaceInfo(hikFaceInfoDto);
			HikFacePicDto hikFacePicDto = new HikFacePicDto();
			hikFacePicDto.setFaceUrl(face.getFaceUrl());
			hikFaceDto.setFacePic(hikFacePicDto);

			try {
				FaceResultVo faceResultVo = hikFaceVService.update(hikFaceDto);
				if (faceResultVo==null){
					return R.failed("接口返回值为空！");
				}
				if (!"0".equals(faceResultVo.getCode())){
					return R.failed("接口调用失败，原因："+faceResultVo.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return R.ok(updateById(face));

	}
	@Resource
	private  FaceMapper faceMapper;

	@Override
	public IPage<Face> queryFaces(Page page, Face face) {
		return faceMapper.queryFaces(page,face);
	}

	@Override
	public HikIndex queryHikIndex() {
		return faceMapper.queryHikIndex();
	}
}
